package com.epro.dx.adapter;

import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.pullview.PullToRefreshListView;
import com.epro.dx.util.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;
import com.weipu.common.facade.model.OrderProductModel;
@SuppressWarnings("ResourceType")
public class BrowseListAdapter extends BaseAdapter{
	/**
	 * 上下文环境
	 */
	private Context mContext;

	/**
	 * list集合
	 */
	private List<OrderProductModel> OrderProductModels;

	public Bitmap defaultBitmap;

	private PullToRefreshListView pullToRefreshListView;

	private ListView listview;

	private boolean isBrowse;
	
	private int densityDpi;
	
	private boolean isWifi;
	
	private  DisplayImageOptions options;
	public BrowseListAdapter(Context context,
			List<OrderProductModel> OrderProductModels,
			PullToRefreshListView pullToRefreshListView, ListView listview,
			boolean isBrowse,int densityDpi,boolean isWifi) {
		super();
		this.mContext = context;
		this.densityDpi=densityDpi;
		this.isWifi=isWifi;
		this.pullToRefreshListView = pullToRefreshListView;
		this.listview = listview;
		this.OrderProductModels = OrderProductModels;
		this.isBrowse = isBrowse;
		if (null != this.pullToRefreshListView && !isBrowse) {
//			this.pullToRefreshListView.setOnScrollListener(this);
		this.pullToRefreshListView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), true, true));
		}
		if (null != this.listview && isBrowse) {
//			this.listview.setOnScrollListener(this);
		this.listview.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), true, true));
		}
		
		options=ImageLoaderUtils.getImageLoaderOptions(R.drawable.item_default,R.drawable.item_default);
	}

	public Context getContext() {
		return mContext;
	}
	
	/**
     * 根据网络连接状态及屏幕像素密度获取图片URL地址
     * @param densityDpi
     * @param product
     * @param defaultDpi1
     * @param defaultDpi2
     * @return
     */
    protected String getImageUrl(int densityDpi,OrderProductModel product,int defaultDpi1,int defaultDpi2){
    	String productIconUrl=null;
    	if(densityDpi>=defaultDpi1){
			if(isWifi){
				 productIconUrl = product.getMediumPictureURL();
			}else{
				 if(densityDpi>=defaultDpi2){
					 productIconUrl = product.getMediumPictureURL();
				 }else{
					 productIconUrl = product.getSmallPictureURL();
				 }
			}
			
		}else{
			 productIconUrl = product.getSmallPictureURL();
		}
    	if(null==productIconUrl||"".equals(productIconUrl)){
    		productIconUrl = product.getSmallPictureURL();
    	}
    	if(null==productIconUrl||"".equals(productIconUrl)){
    		productIconUrl = product.getMediumPictureURL();
    	}
    	return productIconUrl;
    }

	public void removeItem(int p) {
		OrderProductModels.remove(OrderProductModels.get(p));
		notifyDataSetChanged();
	}
	
	public int getProductsSize(){
		return OrderProductModels.size();
	}

	@Override
	public int getCount() {
		return null == OrderProductModels ? 0 : OrderProductModels.size();
	}

	@Override
	public Object getItem(int position) {
		// return null == OrderProductModels ? null :
		// OrderProductModels.get(position);
		if (null != OrderProductModels && OrderProductModels.size() != 0) {
			if (OrderProductModels.size() <= position) {
				return null;
			}
			return OrderProductModels.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.nativebrowselist_item, null);
			viewHolder = new ViewHolder();
			viewHolder.productIcon = (ImageView) convertView
					.findViewById(R.id.img_product);
			viewHolder.productName = (TextView) convertView
					.findViewById(R.id.txt_productname);
			viewHolder.prodcutPrice = (TextView) convertView
					.findViewById(R.id.prodcut_price);
			viewHolder.productShipping = (TextView) convertView
					.findViewById(R.id.txt_shipping);
			viewHolder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.ratingBar);
			viewHolder.reviewCount = (TextView) convertView
					.findViewById(R.id.review_count);
			// viewHolder.productStar =
			// (ImageView)convertView.findViewById(R.id.img_star);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 获取当前显示列表项商品
		OrderProductModel product = (OrderProductModel) OrderProductModels
				.get(position);
		String url =null;
		if(isBrowse){
			url=getImageUrl(densityDpi,product,186,240);
		}else{
			url = product.getSmallPictureURL();
		}
		if (URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) {
			// imageWorker.loadImage(url, viewHolder.productIcon);
			viewHolder.productIcon.setTag(url);
//			boolean isCache=setImageViewByCache(viewHolder.productIcon, url, imageCache,
//					defaultBitmap);
//			if(!isCache&&position<6){
//				imageWorker.loadImage(url, viewHolder.productIcon);
//			}
			 ImageLoader.getInstance().displayImage(url, viewHolder.productIcon, options);
		} else {

			// viewHolder.productIcon.setImageBitmap(ActivityUtil.getDefault488(mContext.getApplicationContext()));
			viewHolder.productIcon.setImageBitmap(getDefault243(mContext));

		}
		// 商品名称
		viewHolder.productName.setText(OrderProductModels.get(position)
				.getShortHeadLine1());
		// 商品价格
//		viewHolder.prodcutPrice.setText(mContext
//				.getString(R.string.product_price1) + product.getPrice());
		String currencyCode = null;
		if(!TextUtils.isEmpty(product.getCurrencyCodeSymbol())){
			currencyCode = product.getCurrencyCodeSymbol();
		}else{
			currencyCode = product.getCurrencyCode();
		}
		viewHolder.prodcutPrice.setText(currencyCode + product.getPrice());
		// 运费
		// viewHolder.productShipping.setText(OrderProductModels.get(position).getFreeShipping()
		// > 0 ? mContext.getString(R.string.product_carriage,
		// OrderProductModels.get(position).getFreeShipping())
		// : mContext.getString(R.string.product_freeship));
		viewHolder.productShipping.setText(R.string.product_freeship_);
		int reviewCount = OrderProductModels.get(position).getReviewCount();
		if (reviewCount > 0) {
			viewHolder.reviewCount.setVisibility(View.VISIBLE);
			viewHolder.reviewCount.setText("(" + reviewCount + " review)");
		} else {
			viewHolder.reviewCount.setVisibility(View.GONE);
		}
		// viewHolder.reviewCount.setText("(" +
		// OrderProductModels.get(position).getReviewCount() + " review)");
		viewHolder.ratingBar.setRating(OrderProductModels.get(position)
				.getProductRating());

		return convertView;
	}

	static class ViewHolder {

		TextView productName;

		TextView prodcutPrice;

		TextView productShipping;

		ImageView productIcon;

		RatingBar ratingBar;

		TextView reviewCount;
		// ImageView productStar;
	}


	/**
	 * 获取默认图片
	 * 
	 * @param context
	 */
	public Bitmap getDefault243(Context context) {
		if (null == defaultBitmap) {
			InputStream is = mContext.getResources().openRawResource(
					R.drawable.item_default);
			final BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 1;
			defaultBitmap = BitmapFactory.decodeStream(is, null, options);
		}
		return defaultBitmap;
	}

}
